package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 预约设置服务
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    //批量导入预约设置数据
    public void add(List<OrderSetting> data) {
        if (data != null && data.size() > 0) {
            for (OrderSetting orderSetting : data) {
                    //检查日期是否重复
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    //执行更新操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    //导入数据
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    //根据月份查询对应的预约的数据
    public List<Map> getOrderSettingByMonth(String date) {
        //封装一个日期范围的Map集合
        String begin=date+"-1";
        String end = date+"-31";
        Map<String, String> map = new HashMap<String, String>();
        map.put("begin",begin);
        map.put("end",end);
        //根据日期范围查询
        List<OrderSetting> list=orderSettingDao.getOrderSettingByMonth(map);
        List<Map> result = new ArrayList<Map>();
        if (list!=null&&list.size()>0){
            for (OrderSetting orderSetting : list){
                Map<String, Object> m = new HashMap<String, Object>();
                m.put("date",orderSetting.getOrderDate().getDate());//获取日期数字几号
                m.put("number",orderSetting.getNumber());//获取预约人数
                m.put("reservations",orderSetting.getReservations());//获取预约人数上限
                result.add(m);
            }
        }
        return result;
    }

    //根据日期设置对应的预约数据
    public void editNumberByDate(OrderSetting orderSetting) {
        //判断根据日期查询是否已经进行预约设置
        Date orderDate = orderSetting.getOrderDate();
        long count = orderSettingDao.findCountByOrderDate(orderDate);
        if (count>0){
            //已经进行预约设置更新预约设置
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            //没有进行预约设置，添加预约设置
            orderSettingDao.add(orderSetting);
        }
    }
}
